package com.qk.controller;

import com.qk.commen.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {


/*    @RequestMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile file) throws Exception {
        log.info("上传文件开始:{}",file);
        //        1.构造文件名
        String fileName=file.getOriginalFilename();//原始文件名
        String extName=fileName.substring(fileName.lastIndexOf("."));//文件后缀 .png
        String newFileName= UUID.randomUUID().toString()+extName;
            //        2.文件保存在磁盘
        file.transferTo(new File("D:\\text\\img"+fileName));
        //构造文件,不能重复-->使用uuid
        return Result.success();
    }*/
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
     public Result upload( MultipartFile image) throws Exception {
        log.info("上传文件开始:{}",image);
        //存在服务端的名字
        String fileName=image.getOriginalFilename();//原始文件名
        String extName=fileName.substring(fileName.lastIndexOf("."));//文件后缀 .png
        String newFileName= UUID.randomUUID().toString()+extName;

       String url= aliyunOSSOperator.upload(image.getBytes(),newFileName);
       return Result.success(url);
    }


}
